//
//  AYMBaseWebViewController.m
//  QuickCure
//
//  Created by xuNick on 16/5/7.
//  Copyright © 2016年 ayumi. All rights reserved.
//

#import "AYMBaseWebViewController.h"

@interface AYMBaseWebViewController ()<UIWebViewDelegate>

@property (nonatomic,weak)UIWebView *webView;

@end

@implementation AYMBaseWebViewController

-(instancetype)initWithUrlString:(NSString *)string andTitle:(NSString *)title{
    if (self = [super init]) {
        self.title = title;
        self.urlString = string;
    }
    return self;
}

#pragma mark - 视图生命周期
-(void)dealloc{
    [SVProgressHUD dismiss];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //添加左侧返回按钮
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"JianTou"] style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
    self.navigationItem.leftBarButtonItem = backItem;
    
    //添加webView
    UIWebView *webView = [[UIWebView alloc]init];
    webView.delegate = self;
    self.webView = webView;
    [self.view addSubview:webView];
    [webView makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    NSURL *url = [NSURL URLWithString:self.urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    Reachability *rea = [Reachability reachabilityWithHostName:self.urlString];
    if (rea.isReachable) {
        [self.webView loadRequest:request];
    }else{
        [SVProgressHUD showErrorWithStatus:@"连接失败"];
    }
    
    }

-(void)backClick{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - webView代理

-(void)webViewDidStartLoad:(UIWebView *)webView{
    [SVProgressHUD showWithStatus:@"正在拼命加载"];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    [SVProgressHUD dismiss];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [SVProgressHUD dismiss];
    [SVProgressHUD showErrorWithStatus:@"连接失败"];
}



@end
